package com.qf.smsplatfrom.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatfrom.cache.NotifyCaChe;
import com.qf.smsplatfrom.constans.ResultCode;
import com.qf.smsplatfrom.events.UpdateNotifyEvents;
import com.qf.smsplatfrom.exceptions.PhoneBaseException;
import com.qf.smsplatfrom.mapper.NotifyMapper;
import com.qf.smsplatfrom.pojo.CheckType;
import com.qf.smsplatfrom.pojo.MyAuthUser;
import com.qf.smsplatfrom.pojo.TNotify;
import com.qf.smsplatfrom.service.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional
public class NotifyServiceImpl implements NotifyService {
    private NotifyCaChe notifyCaChe;
    private NotifyMapper notifyMapper;
    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setNotifyCaChe(NotifyCaChe notifyCaChe) {
        this.notifyCaChe = notifyCaChe;
    }
    @Autowired
    public void setNotifyMapper(NotifyMapper notifyMapper) {
        this.notifyMapper = notifyMapper;
    }

    @Override
    public void addNotify(TNotify tNotify) {
        /*if(tNotify.isNull(CheckType.ADD)){
            throw new PhoneBaseException("传递的数据不可为空", ResultCode.NO_PERMISSON);
        }*/
        notifyMapper.insertNotify(tNotify);
        context.publishEvent(new UpdateNotifyEvents());
    }

    @Override
    public void removeNotify(List<Integer> ids) {
        notifyMapper.deleteNotify(ids);
        context.publishEvent(new UpdateNotifyEvents());
    }

    @Override
    public PageInfo<TNotify> showAllNotify(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TNotify> allData = notifyCaChe.getAllData();
        MyAuthUser principal = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        //Object user = ((ServletRequestAttributes) requestAttributes).getRequest().getSession().getAttribute("user");
        List<TNotify> collect = allData.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        PageInfo<TNotify> pageInfo = new PageInfo<>(collect);
        return pageInfo;
    }

    @Override
    public void updateNotify(TNotify tNotify) {
        notifyMapper.updateNotify(tNotify);
        context.publishEvent(new UpdateNotifyEvents());
    }
}
